from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64
import datetime
import wmi
import tkinter as tk
from tkinter import scrolledtext, simpledialog

# 加密密钥，长度必须为16、24或32字节，分别对应 AES-128、AES-192和AES-256
encryption_key = b'Hex_Carl_2019_AES_'[:16]  # 确保密钥长度为16字节


# 获取本地MAC地址
def get_local_mac_address():
    try:
        c = wmi.WMI()
        for os in c.Win32_OperatingSystem():
            device_id = os.SerialNumber.strip()
            return device_id
    except Exception as e:
        print(f"Error retrieving device ID: {e}")
        return None

# 加密授权信息
def encrypt_license_info(mac_address, start_date, validity_days):
    try:
        # 构建授权信息
        license_info = f"{mac_address}_{start_date}_{validity_days}"
        # 使用AES算法进行加密
        cipher = AES.new(encryption_key, AES.MODE_CBC)
        ct_bytes = cipher.encrypt(pad(license_info.encode(), AES.block_size))
        iv = base64.b64encode(cipher.iv).decode('utf-8')
        ct = base64.b64encode(ct_bytes).decode('utf-8')
        return iv + ct
    except Exception as e:
        print(f"Error encrypting license info: {e}")
        return None


def show_license_code(license_code):
    root = tk.Tk()
    root.title("License Code")

    # 创建一个 Text 小部件以显示 license_code
    text_widget = scrolledtext.ScrolledText(root, width=40, height=10, wrap=tk.WORD)
    text_widget.pack(fill=tk.BOTH, expand=True)

    # 插入 license_code 到 Text 小部件
    text_widget.insert(tk.END, f"License Code:\n{license_code}")

    # 允许用户选择和复制文本
    text_widget.configure(state='normal')  # 使 Text 小部件可编辑
    text_widget.focus_set()  # 设置焦点到 Text 小部件

    # 添加一个 OK 按钮以关闭窗口
    ok_button = tk.Button(root, text="OK", command=root.destroy, padx=10, pady=5)
    ok_button.pack(pady=5)

    root.mainloop()


def get_mac_address_from_user():
    root = tk.Tk()
    root.withdraw()  # Hide the root window
    mac_address = simpledialog.askstring("MAC Address", "请输入MAC地址:")
    return mac_address


def get_user_input():
    root = tk.Tk()
    root.withdraw()  # Hide the root window

    # 获取 MAC 地址
    mac_address = simpledialog.askstring("MAC Address", "请输入MAC地址:")

    if mac_address:
        # 获取授权日期
        license_date = simpledialog.askstring("License Date", "请输入授权日期 (格式: YYYY-MM-DD):")

        # 获取授权可使用时间
        try:
            duration = int(simpledialog.askstring("License Duration", "请输入授权可使用时间 (天数):"))
        except ValueError:
            duration = 10  # 默认值，如果输入无效

        return mac_address, license_date, duration
    else:
        return None, None, None


def auto_user_license():
    # 获取 MAC 地址
    mac_address = get_local_mac_address()

    license_date = "2025-01-1"

    duration = 180  # 默认值，如果输入无效

    license_code = encrypt_license_info(mac_address, license_date, duration)

    return license_code


def main():
    # 获取用户输入的MAC地址、授权日期和授权可使用时间
    mac_address, license_date, duration = get_user_input()

    if mac_address and license_date:
        # 生成license_code
        license_code = encrypt_license_info(mac_address, license_date, duration)
        print(license_code)  # 在控制台输出license_code

        # 显示license_code在一个窗口中
        show_license_code(license_code)
    else:
        print("未输入MAC地址或授权日期。")


if __name__ == "__main__":
    main()
# output_textbox = scrolledtext.ScrolledText(right_frame, width=40, height=15)
# output_textbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# output_textbox.delete("1.0", tk.END)
# output_textbox.insert(tk.END, license_code + "\n")